package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.dto.param.manager.PmsHotelManagerParamDto;
import com.link2room.soundwave.entity.L2RIpms2Group;
import com.link2room.soundwave.entity.L2RYhHotel;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.dto.result.base.PmsHotelDto;
import com.link2room.soundwave.service.IYhPmsBaseService;
import org.springframework.stereotype.Component;
import yuheng.pms.result.YhHotelInfoDto;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component("yhPmsHotelHandlerFacadeService")
public class YhPmsHotelHandlerFacadeServiceImpl extends AbstractPmsHotelHandlerFacadeServiceImpl{
    @Resource
    private ICommonService commonService;
    @Resource
    private IYhPmsBaseService yhPmsBaseService;

    @Override
    PmsHotelDto findPmsHotel(BaseL2rParamDto baseL2rParamDto) throws Exception {
        L2RYhHotel queryHotel=new L2RYhHotel();
        queryHotel.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
        queryHotel.setLhotelCode(baseL2rParamDto.getLhotelCode());
        L2RYhHotel l2RYhHotel=commonService.findOneEQ(L2RYhHotel.class,queryHotel);
        PmsHotelDto pmsHotelDto=null;
        if(l2RYhHotel!=null){
            pmsHotelDto=new PmsHotelDto();
            pmsHotelDto.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
            pmsHotelDto.setLhotelCode(baseL2rParamDto.getLhotelCode());
            pmsHotelDto.setHotelGroupCode(l2RYhHotel.getGroupCode());
            pmsHotelDto.setHotelCode(l2RYhHotel.getHotelCode());
            pmsHotelDto.setHotelName(l2RYhHotel.getHotelName());
        }
        return pmsHotelDto;
    }

    @Override
    QueryResult<PmsHotelDto> listPmsHotelNotMapping(BaseL2rParamDto baseL2rParamDto) throws Exception {
        L2RIpms2Group queryGroup=new L2RIpms2Group();
        queryGroup.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
        L2RIpms2Group l2RIpms2Group=commonService.findOneEQ(L2RIpms2Group.class,queryGroup);
        if(l2RIpms2Group!=null){
            QueryResult<PmsHotelDto> pmsHotelDtoQueryResult=new QueryResult<>();
            List<PmsHotelDto> pmsHotelList=new ArrayList<>();

            L2RYhHotel queryHotel=new L2RYhHotel();
            queryHotel.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
            QueryResult<L2RYhHotel> l2RYhHotelQueryResult = commonService.findAllEQ(L2RYhHotel.class, queryHotel);
            List<L2RYhHotel> l2RYhHotelList = l2RYhHotelQueryResult.getResultlist();

            QueryResult<YhHotelInfoDto> yhHotelInfoDtoQueryResult = yhPmsBaseService.getYhPmsHotelList(
                    baseL2rParamDto.getLhotelGroupCode(),
                    l2RIpms2Group.getPmsHotelGroupCode(),
                    baseL2rParamDto.getCurrentPage(),
                    baseL2rParamDto.getPageSize());
            Long totalCount=yhHotelInfoDtoQueryResult.getTotalrecord();
            if(yhHotelInfoDtoQueryResult.getResultlist()!=null&&yhHotelInfoDtoQueryResult.getResultlist().size()>0){
                Iterator<YhHotelInfoDto> pmsHotelIterator = yhHotelInfoDtoQueryResult.getResultlist().iterator();
                while (pmsHotelIterator.hasNext()){
                    YhHotelInfoDto yhHotelInfoDto=pmsHotelIterator.next();
                    if(l2RYhHotelList!=null&&l2RYhHotelList.size()>0){
                        for(L2RYhHotel l2RYhHotel:l2RYhHotelList){
                            if(yhHotelInfoDto.getHotelCode().equals(l2RYhHotel.getHotelCode())){
                                pmsHotelIterator.remove();
                                totalCount--;
                            }
                        }
                    }
                }
                PmsHotelDto pmsHotelDto;
                for (YhHotelInfoDto pmsHotel:yhHotelInfoDtoQueryResult.getResultlist()){
                    pmsHotelDto=new PmsHotelDto();
                    pmsHotelDto.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
                    pmsHotelDto.setLhotelCode(baseL2rParamDto.getLhotelCode());
                    pmsHotelDto.setHotelGroupCode(pmsHotel.getGroupCode().toString());
                    pmsHotelDto.setHotelCode(pmsHotel.getHotelCode().toString());
                    pmsHotelDto.setHotelName(pmsHotel.getHotelFullName());
                    pmsHotelList.add(pmsHotelDto);
                }
            }
            pmsHotelDtoQueryResult.setResultlist(pmsHotelList);
            pmsHotelDtoQueryResult.setTotalrecord(totalCount);
            pmsHotelDtoQueryResult.setCurrentPage(baseL2rParamDto.getCurrentPage());
            return pmsHotelDtoQueryResult;
        }else{
            throw new SoundwaveException(SoundwaveSysExceptionEnum.L2R_PMS_GROUP_MAPPING_NOT_FOUNDE);
        }
    }

    @Override
    void savePmsHotel(PmsHotelManagerParamDto pmsHotelManagerParamDto) throws Exception {
        L2RYhHotel l2RYhHotel= ClassConverUtil.copyProperties(pmsHotelManagerParamDto.getPmsHotelDto(),L2RYhHotel.class);
        l2RYhHotel.setGroupCode(pmsHotelManagerParamDto.getPmsHotelDto().getHotelGroupCode());
        l2RYhHotel.setHotelCode(pmsHotelManagerParamDto.getPmsHotelDto().getHotelCode());
        commonService.save(l2RYhHotel);
    }

    @Override
    void delPmsHotel(BaseL2rParamDto baseL2rParamDto) throws Exception {
        commonService.delete(L2RYhHotel.class,baseL2rParamDto.getId());
    }
}
